// import router from '@/router'
import store from '@/store'
import axios from 'axios'
import { Message } from 'element-ui'

const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
service.interceptors.request.use(
  config => {
    if (store.getters.token) {
      config.headers.Authorization = `Bearer ${store.getters.token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
service.interceptors.response.use(
  response => {
    if (response.data instanceof Blob) {
      return response.data
    }
    return response.data
  },
  error => {
    Message({ type: 'error', message: error.message })
    // if (error.response.status === 401) {
    //   // 1. 跳转到登录
    //   router.push('/login')
    //   // 2. 清空用户数据
    //   store.commit('user/REMOVE_TOKEN')
    // }
    // return Promise.reject(error)
  }
)
export default service
